/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.main.controller;

import com.kimstore.bean.Order;
import com.kimstore.gui.inventory.NewProductController;
import com.kimstore.gui.inventory.ViewProductController;
import com.kimstore.bean.Product;
import com.kimstore.factory.DaoFactory;
import com.kimstore.gui.main.Inventory;
import com.kimstore.utilities.CustomTableRowSorter;
import com.kimstore.utilities.PaginateSearchAndFilter;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.decorator.HighlighterFactory;

/**
 *
 * @author Administrator
 */
public class InventoryTabController extends BaseTabController {

    private Inventory parentController;
    private PaginateSearchAndFilter<Product> psaf;

    public InventoryTabController(Inventory parentController) {
        this.parentController = parentController;
    }

    public void populateInventoryTable() {
        populateInventoryTable(psaf.search());
    }

    public void refreshTable() {
        clearInventoryTable();
        populateInventoryTable();
    }

    public void clearInventoryTable() {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0);
    }

    public void createNewProductController() {
        NewProductController newProductController = new NewProductController(this);
        newProductController.displayWindow();
    }

    public void createNewProductControllerCallback(Product product) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.insertRow(model.getRowCount(), new Object[]{product.getName(), product.getSpecifications(), 0, product.getBasePrice(), product.getStatus()});
    }

    public void createViewProductController(String productName) {
        ViewProductController controller = new ViewProductController(this, productName);
        controller.displayWindow();
    }

    @Override
    public void prepareUserInterface() {

        table.setShowGrid(true, true);
        table.addHighlighter(HighlighterFactory.createComputedForegroundHighlighter());
    }

    private void populateInventoryTable(List<Product> products) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0);

        for (Product p : products) {
            model.insertRow(model.getRowCount(),
                    new Object[]{p.getName(),
                        p.getSpecifications(),
                        p.getAvailableStocks(),
                        p.getBasePrice(),
                        p.getStatus()});
        }
    }

    public PaginateSearchAndFilter<Product> getPsaf() {
        return psaf;
    }

    public void setPsaf(PaginateSearchAndFilter<Product> psaf) {
        this.psaf = psaf;
    }

    @Override
    public void bindElements() {
        table.setRowSorter(new CustomTableRowSorter(table.getRowSorter().getModel()) {
            @Override
            public void sort() {
                if (table.isShowing()) {
                    Point p = MouseInfo.getPointerInfo().getLocation();
                    int column = getSelectedColumn(p);
                    if (psaf.isColumnSortable(column)) {
                        populateInventoryTable(psaf.sort(column));
                    }
                }
            }
        });
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                populateInventoryTable(psaf.search());
            }
        });
        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Product> product = psaf.nextPage();
                if (product != null) {
                    populateInventoryTable(product);
                }
            }
        });

        prevButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Product> products = psaf.previousPage();
                if (products != null) {
                    populateInventoryTable(products);
                }
            }
        });
    }

    private int getSelectedColumn(Point point) {
        int mousex = point.x - table.getLocationOnScreen().x;
        int mousey = point.y - table.getLocationOnScreen().y;
        Point mousep = new Point(mousex, mousey);

        return table.columnAtPoint(mousep);
    }
}
